package crm;


/**
 * 本次设计用链表存储CRM的数据，再笨类中记录相关处理的函数
 * @author ZHUAN4
 *
 */
public  class DataHand {
	private Customer head;
	private Customer end;
	
	public Customer getHead() {
		return head;
	}

	public void setHead(Customer head) {
		this.head = head;
	}

	public Customer getEnd() {
		return end;
	}

	public void setEnd(Customer end) {
		this.end = end;
	}

	public  void  initCRM(){
		 
		int[] ids={0,1,2,3,5,4,6,7,8,9};
		String[] names={"Jhon","Harry","Oliver","Catherine","George","Olivia","Kate","Charlotter","Gracie","Harley"};
		String[] sexs={"m","m","w","w","m","w","w","m","m","m"};
		String[] birthdays={"19950701","19950702","19950703","19950704","19950705","19950706","19950707","19950708","19950709","19950710"};
		String[] addresses={"oom201，buildingNo.3,No.12，nanjingroad，BaoShanDistrict " ,
							"Room403,No.37,SiFanResidentialQuarter,BaoShanDistrict " ,
							"No.147#HeiYianStreetthepolicycenterofciviladministrationdepartmentthePeople’RepublicofChina" ,
							"Room201,No.34,Lane125,XiKangRoad(South),HongKouDistrict " ,
							"Room3-101buildingNo.20,TianTan-NanXiLiResidentialChongWenDistrictBeiJingCity " ,
							"Room204buildingNo.102,EastTaiShanResidentialBaoYinCountyJiangSuProvince " ,
							"LiuGang，Room42，ZhongzhouRoad，NanyangCity，HenanProv.China473004" ,
							"1TeamCaiQiChuanXiBeiMiningAreaJiangYouCitySiChuanProvinceChina" ,
							"TheMassesArtCentreXinTaiCityHeBeiProv.China",
							"7GroupLiBeiVillagePingWangTownWeJiangCityJiangSuProvince "	};
		String[] calls={"0270000001","0270000002","0270000003","0270000004","0270000005",
						"0270000006","0270000007","0270000008","0270000009","0270000010"};
		head=new Customer(ids[0],names[0],sexs[0],birthdays[0],addresses[0],calls[0]);
		end=new Customer(ids[9],names[9],sexs[9],birthdays[9],addresses[9],calls[9]);
		Customer tempHead=head;
		for(int i=1;i<=8;i++)
		{
			Customer temp=new Customer(ids[i],names[i],sexs[i],birthdays[i],addresses[i],calls[i]);
			tempHead.setNextCustomer(temp);
			temp.setFrontCustomer(tempHead);
			tempHead=temp;	
		}
		tempHead.setNextCustomer(end);
		end.setFrontCustomer(tempHead);
	}
	
	public int getLengthCRM()
	{
		    int length=1;
		    Customer temp=head;
		    while(temp.getNextCustomer()!=null)
		    {
			    length++;
			    temp=temp.getNextCustomer();
		    }
			return length;
	}
	
	/**
	* get the num of node line at site of index
	* @param head
	* @param index
	* @return
	* @throws Exception
	*/
	public Customer getNodeOfIndex(int index) throws Exception
	{
		int length=this.getLengthCRM();
		if(index>length) throw new Exception("out of index!");
		Customer temp=head;
		for(int i=1;i<=index;i++)
		{
			temp=temp.getNextCustomer();
		}
		return temp;
			
	}
	/**
	 * 查看所有的客户的信息，对应 无参L命令
	 * @throws Exception
	 */
    public  void lookCRM() throws Exception{
//		System.out.printf("there is %s customer!",this.getLengthCRM());
    	System.out.println(" id        name    sex    birthday     call           address ");
    	for(int i=0;i<this.getLengthCRM();i++){
				System.out.printf("%3s",this.getNodeOfIndex( i).getId());
				System.out.printf("%12s",this.getNodeOfIndex( i).getName());
				System.out.printf("%6s   ",this.getNodeOfIndex( i).getSex());
				System.out.printf("%10s",this.getNodeOfIndex( i).getBirthday());
				System.out.printf("%15s     ",this.getNodeOfIndex( i).getCall());
				System.out.printf("%-100s",this.getNodeOfIndex( i).getAddress());
				System.out.println();
    	}
    }

		
	/**
	 * 通过顾客的id来查看顾客的信息，对应有参的L ID命令
	 * @param id
	 * @throws Exception
	 */
	public  void lookByIdCRM(int id) throws Exception{
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM();i++)
		{
			if(this.getNodeOfIndex(i).getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else
		{
			if(index<0&&index>=this.getLengthCRM())
			{
				System.out.println("The index found out of limit!");
			}
			else
			{
				System.out.println(" id        name    sex    birthday     call           address ");
				System.out.printf("%3s",this.getNodeOfIndex( index).getId());
				System.out.printf("%12s",this.getNodeOfIndex(index).getName());
				System.out.printf("%6s   ",this.getNodeOfIndex( index).getSex());
				System.out.printf("%10s",this.getNodeOfIndex( index).getBirthday());
				System.out.printf("%15s     ",this.getNodeOfIndex( index).getCall());
				System.out.printf("%-100s",this.getNodeOfIndex(index).getAddress());
				System.out.println();
			}
		}
	}
	
	/**
	 * 通过顾客的name来查看顾客的信息，对应有参的L ID命令
	 * @param id
	 * @throws Exception
	 */
	public  void lookByNameCRM(String name) throws Exception{
		int flag=0;
		int[] indexs = new int[this.getLengthCRM()];
		int count=0;
		for(int i=0;i<this.getLengthCRM();i++)
		{
			if(this.getNodeOfIndex(i).getName().equals(name))
			{
				flag=1;
				indexs[count]=i;
				count++;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else
		{
			System.out.println(" id        name    sex    birthday     call           address ");
			for(int i=0;i<count;i++)
			{
				System.out.printf("%3s",this.getNodeOfIndex( indexs[i]).getId());
				System.out.printf("%12s",this.getNodeOfIndex(indexs[i]).getName());
				System.out.printf("%6s   ",this.getNodeOfIndex( indexs[i]).getSex());
				System.out.printf("%10s",this.getNodeOfIndex( indexs[i]).getBirthday());
				System.out.printf("%15s     ",this.getNodeOfIndex( indexs[i]).getCall());
				System.out.printf("%-100s",this.getNodeOfIndex(indexs[i]).getAddress());
				System.out.println();
			}
				
			
		}
	}
	
	
	/**
	 * 将target 中的值赋值给 head
	 * @param head
	 * @param target
	 */
	public void exchangeCRM(Customer head, Customer target){
		head.setAddress(target.getAddress());
		head.setBirthday(target.getBirthday());
		head.setCall(target.getCall());
		head.setName(target.getName());
		head.setSex(target.getSex());
		head.setId(target.getId());
	}
	
	/**
	 * 向信息中添加一条数据  对应 A 操作
	 * @param add
	 */
	public void  addCRM(Customer add){
//		System.out.println(this.getLengthCRM());
		if(this.getLengthCRM()==0)
		{
			this.exchangeCRM(head, add);
			Customer.count++;
		}
		else if(this.getLengthCRM()==1)
		{
			this.exchangeCRM(end, add);
			head.setNextCustomer(end);
			end.setFrontCustomer(head);
			Customer.count++;
		}
		else if(this.getLengthCRM()>=2)
		{
			Customer temp1=new Customer();
			this.exchangeCRM(temp1, add);
            end.setNextCustomer(temp1);
            temp1.setFrontCustomer(end);
            end=temp1;   
            Customer.count++;
		}
	}
	/**
	 * 通过id来在链表中查找删除相应的信息
	 * @param id
	 * @throws Exception
	 */
	public void deleteCRM(int id) throws Exception{
//		System.out.println(this.getLengthCRM());
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM();i++)
		{
			if(this.getNodeOfIndex(i).getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==1)
		{
			if(index>=0&&index<this.getLengthCRM())
			{
				if(index==0)
				{
					head=head.getNextCustomer();
					head.setFrontCustomer(null);
				}
				else if(index==this.getLengthCRM()-1)
				{
					end=end.getFrontCustomer();
					end.setNextCustomer(null);
				}
				else
				{
					Customer temp=this.getNodeOfIndex(index);
					temp.getFrontCustomer().setNextCustomer(temp.getNextCustomer());
					temp.getNextCustomer().setFrontCustomer(temp.getFrontCustomer());
					temp.setFrontCustomer(null);
					temp.setNextCustomer(null);
				}
			}
			else
			{
				System.out.println("Index found out of limit!");
			}
		}
		else
		{
			System.out.printf("The customer(id of %s) is not found!",id);
			System.out.println();
		}
					
	}
	
	/**
	 * 更新数据操作根据Id来查找信息并更新为update参数中的内容
	 * @param id
	 * @param update
	 * @throws Exception
	 */
	public void updateCRM(int id,Customer update) throws Exception{
		
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM();i++)
		{
			if(this.getNodeOfIndex(i).getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else{
			if(index>=0&&index<this.getLengthCRM())
			{
				int tempId=this.getNodeOfIndex(index).getId();
				this.exchangeCRM(this.getNodeOfIndex(index), update);
				this.getNodeOfIndex(index).setId(tempId);
			}
			else
			{
				System.out.println("Index found out of limit!");
			}
		}	
	}
	
	/**
	 * 根据Id的大小排序
	 * @throws Exception
	 */
	public void sortByIdCRM() throws Exception{
		int length=this.getLengthCRM();
		for(int i=length;i>1;i--)
		{
			for(int j=0;j<i-1;j++)
			{
				if(this.getNodeOfIndex(j).getId()>this.getNodeOfIndex(j+1).getId())
				{
					Customer temp=new Customer();
					this.exchangeCRM(temp, this.getNodeOfIndex(j));
					this.exchangeCRM(this.getNodeOfIndex(j), this.getNodeOfIndex(j+1));
					this.exchangeCRM(this.getNodeOfIndex(j+1), temp);
				}
			}
		}
	}
	
	/**
	 * 根据name的大小排序
	 * @throws Exception
	 */
	public void sortByNameCRM() throws Exception{
		int length=this.getLengthCRM();
		for(int i=length;i>1;i--)
		{
			for(int j=0;j<i-1;j++)
			{
				if(this.getNodeOfIndex(j).getName().compareTo(this.getNodeOfIndex(j+1).getName())>0)
				{
					Customer temp=new Customer();
					this.exchangeCRM(temp, this.getNodeOfIndex(j));
					this.exchangeCRM(this.getNodeOfIndex(j), this.getNodeOfIndex(j+1));
					this.exchangeCRM(this.getNodeOfIndex(j+1), temp);
				}
			}
		}
	}

}
